{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 再次调用xgboost内嵌的cv寻找最佳的参数n_estimators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = pd.read_csv(\"RentListingInquries_FE_train.csv\")\n",
    "y_train = train['interest_level']\n",
    "X_train = train.drop([ \"interest_level\"], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你学习率改小成0.02，调整弱分类数目\n",
    "此前已经调好的参数： n_estimators：198 max_depth：6 min_child_weight：0.01 reg_alpha：1 reg_lambda：1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def modelfit(alg, X_train, y_train, cv_folds=3, early_stopping_rounds=10):\n",
    "    xgb_param = alg.get_xgb_params()\n",
    "    xgb_param['num_class'] = 3\n",
    "    xgtrain = xgb.DMatrix(X_train, label = y_train)\n",
    "    cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], folds =cv_folds,\n",
    "             metrics='mlogloss', early_stopping_rounds=early_stopping_rounds)\n",
    "    cvresult.to_csv('6_nestimators.csv', index_label = 'n_estimators')\n",
    "    n_estimators = cvresult.shape[0]\n",
    "    # 采用交叉验证得到的最佳参数n_estimators，训练模型\n",
    "    alg.set_params(n_estimators = n_estimators)\n",
    "    alg.fit(X_train, y_train, eval_metric='mlogloss')\n",
    "    #Predict training set:\n",
    "    train_predprob = alg.predict_proba(X_train)\n",
    "    logloss = log_loss(y_train, train_predprob)\n",
    "   #Print model report:\n",
    "    print ('logloss of train is: %s'%(logloss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of train is: 0.469190382899\n"
     ]
    }
   ],
   "source": [
    "xgb6 = XGBClassifier(learning_rate =0.02,n_estimators=2000,max_depth=6,min_child_weight=0.01,gamma=0, \\\n",
    "                     subsample = 0.5,colsample_bytree=0.8,colsample_bylevel=0.7,reg_alpha =1,reg_lambda =1, \\\n",
    "                     objective= 'multi:softprob',seed=3)\n",
    "modelfit(xgb6, X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'base_score': 0.5,\n",
       " 'booster': 'gbtree',\n",
       " 'colsample_bylevel': 0.7,\n",
       " 'colsample_bytree': 0.8,\n",
       " 'gamma': 0,\n",
       " 'learning_rate': 0.02,\n",
       " 'max_delta_step': 0,\n",
       " 'max_depth': 6,\n",
       " 'min_child_weight': 0.01,\n",
       " 'missing': None,\n",
       " 'n_estimators': 1137,\n",
       " 'nthread': 1,\n",
       " 'objective': 'multi:softprob',\n",
       " 'reg_alpha': 1,\n",
       " 'reg_lambda': 1,\n",
       " 'scale_pos_weight': 1,\n",
       " 'seed': 3,\n",
       " 'silent': 1,\n",
       " 'subsample': 0.5}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xgb6.get_xgb_params()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: from_csv is deprecated. Please use read_csv(...) instead. Note that some of the default arguments are different, so please refer to the documentation for from_csv when changing your function calls\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEXCAYAAABCjVgAAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3Xl8XHW9//HXJ5OtaZJuSfd9YSkUClRWBWSH6wVFRCqgoFfk/kQB5XJBvYp4uS7XfRcQuW4gggoiiMqqqEBbCrRA931Nl7RJ26RZPr8/vifpJM0yaTM5M5n38/E4j8xZ5pzPmZPMO2f7HnN3REREAPLiLkBERDKHQkFERFopFEREpJVCQUREWikURESklUJBRERaKRREkpjZp8zs7rjrEImLQiHLmFmpma00s8uThpWZ2WozuyRp2Cwze9TMtptZtZm9bmZ3mNmQaPxVZtZkZrVRt9zM/j3NtZ9uZmvTuYye6Kged/8fd/+3NC1vpZmdlY55p0Nfba9s+1z6O4VClnH3WuAjwDfNrDIa/BVgjrs/CGBmJwPPAM8Dh7n7YOA8oBE4Oml2/3D3UncvBd4NfMXMjumbNZGeMLP8uGuQHOHu6rKwA+4F7gNOB7YCI5PG/Q34Tjfvvwr4W7thLwLvS+q/EFgIVBNC5vCkcYdHw6qjaS5MGncB8DpQA6wDbgIGAnuAZqA26kZ3sl7fA/4Qvf8FYEoKn8dhwJ+BbcAi4NIDqQe4Dfh59L6JgANXA2uA7cC1wFuAV6N1/27ScqYAT0XbYwvwC2BwNO5n0bL2RMu6OYXPeCXwn9Gy6oH8qH9dtC6LgDM7+CxOADYCiaRh7wJejV4fD8wBdgKbgK938pmeDqztZNwg4KdAFbAK+AyQF41LAF+LPoMVwHXR55jfybxWAmd1Mu7DwNJouz7S8jsDGPANYHO0Hq8BR3a2veP+e82mLvYC1B3gho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UEM4LvAL8r5lNI5wjeDIathq4JTrU9MWeLsjdXzGzlwnNT68hHKJqcSfwRzNb7+5vjy51fTpa/h/c/eH950gZ8LCZFUfTfaKnNYl0RJekiohIKx0+EhGRVjp8JP2emX2P8EzhZN9y95/EUY9IJtPhIxERaaXDRyIi0kqhICIirRQKIiLSSqEgIiKt/j/DYZ+UM70dRwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x11501df98>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cvresult = pd.DataFrame.from_csv('6_nestimators.csv')\n",
    "\n",
    "test_means = cvresult['test-mlogloss-mean']\n",
    "test_stds = cvresult['test-mlogloss-std']     \n",
    "train_means = cvresult['train-mlogloss-mean']\n",
    "train_stds = cvresult['train-mlogloss-std'] \n",
    "\n",
    "x_axis = range(0, cvresult.shape[0])\n",
    "        \n",
    "pyplot.errorbar(x_axis, test_means, yerr=test_stds ,label='Test')\n",
    "pyplot.errorbar(x_axis, train_means, yerr=train_stds ,label='Train')\n",
    "pyplot.title(\"XGBoost n_estimators vs Log Loss\")\n",
    "pyplot.xlabel( 'n_estimators' )\n",
    "pyplot.ylabel( 'Log Loss' )\n",
    "pyplot.savefig( 'n_estimators6.png' )\n",
    "pyplot.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最佳n_estimator的值为1136"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 保存模型，供测试使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#保存模型\n",
    "import pickle\n",
    "pickle.dump(xgb6, open(\"xgb_model.pkl\", 'wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of train is: 0.469190382899\n"
     ]
    }
   ],
   "source": [
    "#保存数据\n",
    "import pickle\n",
    "xgb = pickle.load(open(\"xgb_model.pkl\", 'rb'))\n",
    "train_predprob = xgb.predict_proba(X_train)\n",
    "logloss = log_loss(y_train, train_predprob)\n",
    "print('logloss of train is: %s'%(logloss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
